package
{
	import as3isolib.core.IIsoDisplayObject;
	import as3isolib.display.IsoSprite;
	import as3isolib.display.IsoView;
	import as3isolib.display.primitive.IsoBox;
	import as3isolib.display.scene.IsoGrid;
	import as3isolib.display.scene.IsoScene;
	import as3isolib.geom.IsoMath;
	import as3isolib.geom.Pt;
	import as3isolib.utils.IsoDrawingUtil;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.geom.Vector3D;

	public class Global
	{	
		public static var originVector:Vector3D = new Vector3D(0,0,0);
		public static var highlightFilter:GlowFilter = new GlowFilter(0x0077ff);
		public static const PATH_NODE_SIZE:int = .2 * Constants.SQUARE_SIZE;
		
		public static var stage:DisplayObjectContainer;
		public static var player:Player = new Player();
		public static var actionBarManager:ActionBarManager;
		public static var stores:Vector.<GameObject> = new Vector.<GameObject>;
		public static var isoView:IsoView = new IsoView();
		public static var isoScene:IsoScene  = new IsoScene();
		public static var roadScene:IsoScene = new IsoScene();
		public static var inputManager:InputManager;
		public static var simulationManager:SimulationManager = new SimulationManager();
		public static var fps:Number;
		
		public static var pathManager:PathManager = new PathManager();
		public function Global()
		{
			
		}
		
		public static function changeCitizenPathingStrategy():void{
			Global.player.changeCitizenPathingStrategy();
		}
		
		public static function onEnterFrame(e:Event):void{
			Global.simulationManager.update();
			isoScene.render();
		}
		
		
		public static function getRectSprite(width:int,height:int,color:uint=0x000000,alpha:Number=1.0,x:int=0,y:int=0,spriteToRedraw:Sprite=null):Sprite{
			var rect:Sprite = (spriteToRedraw) ? spriteToRedraw : new Sprite();
			rect.graphics.clear();
			rect.graphics.beginFill(color, alpha);
			rect.graphics.drawRect(x,y,width,height);
			rect.graphics.endFill()
			return rect;
		}
		
		public static function pt2Node(pt:Pt):PathNode{
			return new PathNode(new Vector3D(pt.x, pt.y, pt.z));
		}
		
		public static function getPathNodeImage():IsoBox{
			var box:IsoBox = new IsoBox();
			box.setSize(PATH_NODE_SIZE, PATH_NODE_SIZE, PATH_NODE_SIZE);
			return box;
		}
		
		public static function getIsoRectSprite(startPoint:Vector3D, endPoint:Vector3D):IsoSprite{
			var sprite:Sprite = new Sprite();
			var g:Graphics = sprite.graphics;
			var originPt:Pt = new Pt(startPoint.x, startPoint.y, startPoint.z);
			var deltaVector:Vector3D = endPoint.subtract(startPoint);
			var width:Number = deltaVector.x;
			var length:Number = deltaVector.y;
			var isoSprite:IsoSprite = new IsoSprite();
			
			isoSprite.container.addChild(sprite);
			g.lineStyle(.5);
			IsoDrawingUtil.drawIsoRectangle(g, originPt, width, length, "xy");
			//g.endFill();
			
			return isoSprite;
		}
		
		public static function getIsoLineSprite(startPoint:Vector3D, endPoint:Vector3D):IsoSprite{
			var isoSprite:IsoSprite = new IsoSprite();
			var sprite:Sprite = new Sprite();
			var pointFromOrigin:Vector3D = endPoint.subtract(startPoint);
			var screenPoint:Point = IsoMath.isoToScreen(new Pt(pointFromOrigin.x, pointFromOrigin.y, pointFromOrigin.z));
			sprite.graphics.beginFill(0);
			sprite.graphics.lineStyle(.5);
			sprite.graphics.lineTo(screenPoint.x, screenPoint.y);
			sprite.graphics.endFill();
			isoSprite.container.addChild(sprite);
			isoSprite.setSize(pointFromOrigin.x, pointFromOrigin.y, pointFromOrigin.z);
			isoSprite.x = startPoint.x;
			isoSprite.y = startPoint.y;
			isoSprite.z = startPoint.z;
			
			return isoSprite;
		}
		
		
		public static function createGrid():IsoGrid{
			var grid:IsoGrid = new IsoGrid();
			grid.setGridSize(20,20);
			grid.cellSize = Constants.SQUARE_SIZE;
			return grid;
		}
		
		public static function setLocation(isoDisplay:IIsoDisplayObject, location:Vector3D):void{
			isoDisplay.x = location.x;
			isoDisplay.y = location.y;
			isoDisplay.z = location.z;
		}
	}
}